package com.example.thridhero.litepaltest;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button createDbBtn;
    private Button addDataBtn;
    private Button saveUpdateDataBtn;
    private Button updateDataBtn;
    private Button deleteDataBtn;
    private Button readDataBtn;
    private Button readFirstDataBtn;
    private Button readLastDataBtn;
    private Button readLineBtn;
    private Button readSelBtn;

    public static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        createDbBtn = (Button) findViewById(R.id.create_database);
        addDataBtn = (Button) findViewById(R.id.add_data);
        saveUpdateDataBtn = (Button) findViewById(R.id.save_update_data);
        updateDataBtn = (Button) findViewById(R.id.update_data);
        createDbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Connector.getDatabase();
            }
        });
        deleteDataBtn = (Button) findViewById(R.id.delete_data);
        readDataBtn = (Button) findViewById(R.id.read_data);
        readFirstDataBtn = (Button) findViewById(R.id.read_first_data);
        readLastDataBtn = (Button) findViewById(R.id.read_last_data);
        readLineBtn = (Button) findViewById(R.id.read_line);
        readSelBtn = (Button) findViewById(R.id.read_sel);


        addDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book= new Book();
                book.setName("The Da Vinci Code");
                book.setAuthor("Dan Brown");
                book.setPages(454);
                book.setPrice(19.96);
                book.setPress("Unknow");
                book.save();

                Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
            }
        });
        saveUpdateDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book= new Book();
                book.setName("The Da Vinci Code1");
                book.setAuthor("Dan Brown");
                book.setPages(454);
                book.setPrice(19.96);
                book.setPress("Unknow");
                book.save();

                book.setPrice(20);
                book.save();
            }
        });
        updateDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setPrice(14.95);
                book.setPress("anchor");
                book.updateAll("name = ? and author = ? ","The Da Vinci Code","Dan Brown");
            }
        });
        deleteDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class,"price < ?","15");
            }
        });
        readDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> books = DataSupport.findAll(Book.class);
                for (Book book:books){
                    Log.d("MainActivity", "onClick: "+book.toString());
                }
            }
        });
        readFirstDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book firstBook = DataSupport.findFirst(Book.class);
                Log.d("MainActivity", "onClick: "+firstBook.toString());
            }
        });
        readLastDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book lastBook = DataSupport.findLast(Book.class);
                Log.d(TAG, "onClick: "+lastBook.toString());
            }
        });
        readLineBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> books = DataSupport.select("name","author").find(Book.class);
                for (Book book :books){
                    Log.d(TAG, "onClick: "+book.toString());
                }
            }
        });
        readSelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> books = DataSupport.where("pages > ?","400").find(Book.class);
                for (Book book :books){
                    Log.d(TAG, "where 查询: "+book.toString());
                }

                List<Book> books2 = DataSupport.order("price desc").find(Book.class);
                for (Book book :books2){
                    Log.d(TAG, "order 查询: "+book.toString());
                }

                List<Book> books3 = DataSupport.limit(3).find(Book.class);
                for (Book book :books3){
                    Log.d(TAG, "limit 查询: "+book.toString());
                }

                List<Book> books4 = DataSupport.limit(3).offset(1).find(Book.class);
                for (Book book :books4){
                    Log.d(TAG, "limit offset 查询: "+book.toString());
                }


                List<Book> books5 = DataSupport.select("name","author","pages")
                        .where("pages > ? ","400")
                        .order("pages")
                        .limit(10)
                        .offset(10)
                        .find(Book.class);

                for (Book book :books5){
                    Log.d(TAG, "select 语句查询: "+book.toString());
                }

                Cursor c = DataSupport.findBySQL(" select * from Book where pages > ? " +
                        "and price < ?","400","20");

                List<Book> books6 = new ArrayList<Book>();
                if(c.moveToFirst()){
                    do{
                        int id  = c.getInt(c.getColumnIndex("id"));
                        String author = c.getString(c.getColumnIndex("author"));
                        double price = c.getDouble(c.getColumnIndex("price"));
                        int pages = c.getInt(c.getColumnIndex("pages"));
                        String name = c.getString(c.getColumnIndex("name"));
                        Book book = new Book();
                        book.setId(id);
                        book.setAuthor(author);
                        book.setPages(pages);
                        book.setPrice(price);
                        book.setName(name);
                        books6.add(book);
                    }while (c.moveToNext());
                }
                for (Book book :books6){
                    Log.d(TAG, "指定sql语句查询: "+book.toString());
                }
            }
        });
    }

}
